package com.ag.lzw.service.impl;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ag.bean.TbPropose;
import com.ag.contactTree.TreeNode;
import com.ag.contactTree.service.LocalTreeService;
import com.ag.lwc.service.FindfDepService;
import com.ag.lzw.dao.LzwTbProposeDAO;
import com.ag.lzw.service.IdentifyService;
import com.ag.zhh.vo.UserDept;
import com.ag.zhh.vo.UserDeptContact;

@Service("IdentifyServiceImpl")
public class IdentifyServiceImpl implements IdentifyService {

	private static final Logger logger = Logger.getLogger(IdentifyServiceImpl.class);
	
	@Resource(name="lzwTbProposeDAO")
	private LzwTbProposeDAO proposeDAO;
	@Resource(name="FindfDepService")
	private FindfDepService findDeptSer;
	@Resource(name = "LocalTreeService")
	private LocalTreeService lts;
	
	@Override
	public boolean isCenterDeptPeople(String uid, String did) {
		logger.info(">>>finding user:"+uid+" in dept:"+did);
		//查找该did下直属人员的信息
		List<UserDeptContact> udcs = findDeptSer.findDeptsUsers(did);
		for(UserDeptContact udc : udcs){
			if(udc.getUId().equals(uid)){
				return true;
			}
		}
		//从通信树中查找该did的子部门
		List<TreeNode> nodes = lts.getChildrenByFDId(did);
		for(TreeNode node : nodes){
			UserDept dept = (UserDept)node.getBindData();
			//取出该子部门的从属人员
			Set<UserDeptContact> users = dept.getTbUsers();
			for(UserDeptContact user:users){
				if(user.getUId().equals(uid)){
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public boolean isLeader(String uid, Object obj) {
		String eventID = obj.toString(); // 获取当前处置事件的ID
		logger.info(">>>Event ID:" + eventID);
		List list = proposeDAO.findEventLeader(eventID);// 查询当前事件的指挥长
		if (list != null) {
			for (Object obj2 : list) {
				TbPropose tp = (TbPropose) obj2; // 判断当前用户是否事件的指挥长
				if (tp.getTbUserByPViceCommander().getUId().equals(uid)) {
					logger.info(">>>It is Leader, TB_U_ID:"+ tp.getTbUserByPViceCommander().getUId());
					return true;
				}
			}
		}
		return false;
	}

}
